Skip to content

Method: addConfiguration(Map, Collection)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.config;
16:
17: import java.util.Collection;
18: import java.util.Map;
19: import java.util.Objects;
20: import java.util.concurrent.ConcurrentHashMap;
21:
22: /**
23: * Holds configuration of the OntoDriver.
24: */
25: public final class Configuration {
26: // TODO Tests
27:
28: private final ConcurrentHashMap<ConfigurationParameter, Object> configuration = new ConcurrentHashMap<>();
29:
30: /**
31: * Loads configuration of parameters specified by {@code parameters} from the provided properties map.
32: *
33: * @param properties Map of configuration values
34: * @param parameters Parameters to extract from the map
35: */
36: public void addConfiguration(Map<String, String> properties, Collection<ConfigurationParameter> parameters) {
37: Objects.requireNonNull(properties);
38: Objects.requireNonNull(parameters);
39: parameters.stream().filter(p -> properties.containsKey(p.toString()))
40: .forEach(p -> setProperty(p, properties.get(p.toString())));
41: }
42:
43: /**
44: * Sets configuration of the specified parameter to the specified value.
45: * <p>
46: * Overrides any previous setting.
47: *
48: * @param property Parameter
49: * @param value Value
50: */
51: public void setProperty(ConfigurationParameter property, Object value) {
52: Objects.requireNonNull(property);
53: configuration.put(property, value);
54: }
55:
56: /**
57: * Gets value of the specified property.
58: *
59: * @param property Parameter
60: * @return Value of the property or {@code null}, if it is not set
61: */
62: public Object getProperty(ConfigurationParameter property) {
63: Objects.requireNonNull(property);
64: return configuration.get(property);
65: }
66:
67: /**
68: * Gets value of the specified property or the default value, if the property is not set.
69: *
70: * @param property Parameter
71: * @param defaultValue Value to return if the property is not set
72: * @return Value of the property or {@code defaultValue}, if it is not set
73: */
74: public Object getProperty(ConfigurationParameter property, Object defaultValue) {
75: Objects.requireNonNull(property);
76: return configuration.getOrDefault(property, defaultValue);
77: }
78:
79: /**
80: * Returns value of the specified property as the specified type (if it can be cast to it).
81: *
82: * @param property Parameter
83: * @param targetType Target type
84: * @return Property value or {@code null}, if it is not set
85: * @throws IllegalArgumentException if property value cannot be cast to {@code targetType}
86: */
87: public <T> T getProperty(ConfigurationParameter property, Class<T> targetType) {
88: if (configuration.containsKey(property)) {
89: final Object value = configuration.get(property);
90: if (value != null && targetType.isAssignableFrom(value.getClass())) {
91: return targetType.cast(value);
92: } else {
93: throw new IllegalArgumentException(
94: "Cannot return value " + value + " of property " + property + " as type " + targetType);
95: }
96: }
97: return null;
98: }
99:
100: /**
101: * Returns value of the specified property as the specified type (if it can be cast to it).
102: *
103: * @param property Parameter
104: * @param targetType Target type
105: * @param defaultValue Value to return if the property is not set
106: * @return Value of the property or {@code defaultValue}, if it is not set
107: * @throws IllegalArgumentException if property value cannot be cast to {@code targetType}
108: */
109: public <T> T getProperty(ConfigurationParameter property, Class<T> targetType, T defaultValue) {
110: final Object value = configuration.getOrDefault(property, defaultValue);
111: if (value != null && targetType.isAssignableFrom(value.getClass())) {
112: return targetType.cast(value);
113: } else {
114: throw new IllegalArgumentException(
115: "Cannot return value " + value + " of property " + property + " as type " + targetType);
116: }
117: }
118: }